﻿using System.Collections.Generic;

namespace Kernelx.Contexts.AspNetCoreImpls
{
    using Core;
    using Kernelx.Extensions; 
    using System.Linq;

    internal static class RequsetParameter
    {
        protected class RequsetParameterDict : RequestParameterCollectionBase
        {
            public RequsetParameterDict(IDictionary<string, object> parameters) : base(() => parameters) { }
        }

        protected class RequsetParameterObject : RequestParameterCollectionBase
        { 
            public RequsetParameterObject(object parameters) : base(() => Convert(parameters)) {}

            private static IDictionary<string, object> Convert(object parameters)
            {
                var type = parameters.GetType();
                var props = type.GetProperties().Where(x => x.CanRead).ToList();
                return props.ToDictionary(kv => kv.Name, kv => kv.GetValue(parameters, kv.GetIndexValues()));
            }
        }




        public static IRequestParameterCollection Create(object parameters)
            => parameters is IDictionary<string, object> dict ? new RequsetParameterDict(dict) : new RequsetParameterObject(parameters);

    }
}
